﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Emerson.Storage.Service.Interface;
using Emerson.Storage.Entities;

namespace WorkstationDashboard
{
    public partial class AddWorkstation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            findButton.Click += new EventHandler(findButton_Click);
            addToGroupButton.Click += new EventHandler(addToGroupButton_Click);
            addToGroupPanel.Visible = false;
            msg.InnerText = "";
        }

        void addToGroupButton_Click(object sender, EventArgs e)
        {
            int groupid = int.Parse(groupDropList.SelectedValue);
            IWorkstationService wsService = ServiceHelper.GetService<IWorkstationService>();
            addToGroupPanel.Visible = true;

            try
            {
                Workstation wk = wsService.FindWorkstation(addressTextBox.Text.Trim());
                wsService.AddWorkstation(groupid, wk);
                msg.InnerText = "Workstation is added";
            }
            catch (Exception ex)
            {
                msg.InnerText = ex.Message;
            }
        }

        void findButton_Click(object sender, EventArgs e)
        {
            IWorkstationService wsService = ServiceHelper.GetService<IWorkstationService>();
            try
            {
                Workstation wk = wsService.FindWorkstation(addressTextBox.Text.Trim());
                workstationName.Text = wk.Name;
                workstationAddress.Text = addressTextBox.Text;
                addToGroupPanel.Visible = true;
                WorkstationRoot wr = wsService.GetWorkstationRoot();

                groupDropList.DataTextField = "Name";
                groupDropList.DataValueField = "GroupId";
                groupDropList.DataSource = wr.Groups;
                groupDropList.DataBind();
            }
            catch (Exception ex)
            {
                msg.InnerText = ex.Message;
                addToGroupPanel.Visible = false;
            }
        }
    }
}
